package com.trt.eatmeat.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.trt.eatmeat.sign.DockingModelForGateway;
import com.trt.eatmeat.sign.SignUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yufeng_ma@trtjk.com
 * @Description //TODO
 * @Date 2022/9/28 9:04
 **/
@Controller
public class JsonSignController {

    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping(value = "/toSign")
    public  String toSign( Model model){
        model.addAttribute("str","");
        model.addAttribute("secret","");
        model.addAttribute("sign","");
        return "sign";
    }


    @PostMapping(value = "/toSign")
    public  String toSign(@RequestParam("str") String json,@RequestParam("secret") String secret,  Model model){
        model.addAttribute("str",json);
        model.addAttribute("secret",secret);
        try {

            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            DockingModelForGateway dockingModelForGateway= objectMapper.readValue(json, DockingModelForGateway.class);
            if(dockingModelForGateway.getDeveloperId() == null){
                model.addAttribute("sign", "开发者developerId不能为空");
                return "sign";
            }
            if(StringUtils.isEmpty(dockingModelForGateway.getCharset())){
                model.addAttribute("sign", "charset不能为空");
                return "sign";
            }

            if(dockingModelForGateway.getTimestamp() ==null){
                model.addAttribute("sign", "timestamp不能为空");
                return "sign";
            }

            if(StringUtils.isEmpty(dockingModelForGateway.getVersion())){
                model.addAttribute("sign", "version不能为空");
                return "sign";
            }
            //验签
            Map<String,String> hashMap = new HashMap<>();
            hashMap.put("biz",dockingModelForGateway.getBiz()) ;
            hashMap.put("developerId",dockingModelForGateway.getDeveloperId().toString());
            hashMap.put("charset",dockingModelForGateway.getCharset());
            hashMap.put("timestamp",dockingModelForGateway.getTimestamp().toString());
            hashMap.put("version",dockingModelForGateway.getVersion());
            hashMap.put("businessType",dockingModelForGateway.getBusinessType().toString());
            String sign = SignUtils.createSign(secret, hashMap);
            model.addAttribute("sign", sign);
            return "sign";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("sign", "非json格式");
            return "sign";
        }
    }

}
